﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EricUtility.Windows.Forms;
using System.Threading;
using EbayQueryExpertLib;
using EbayQueryExpertLib.Entities;
using EbayQueryExpertLib.EbayFinding;
using EricUtility;
using EricUtility.Networking.Gathering;
using System.Globalization;

namespace EbayQueryExpert
{
    public partial class TestCompareForm : Form
    {
        Dictionary<int, string> Urls = new Dictionary<int, string>();
        Dictionary<int, Label> LabelURL = new Dictionary<int, Label>();
        Dictionary<int, Label> LabelAPI = new Dictionary<int, Label>();
        Dictionary<int, WaitPictureBox> WaitPB = new Dictionary<int, WaitPictureBox>();
        public TestCompareForm(Dictionary<int, string> dUrls, Dictionary<int, string> dDescs)
        {
            InitializeComponent();
            //Height += 15 * dUrls.Count;
            int i = 0;
            foreach (int id in dUrls.Keys)
            {
                Label l = new Label();
                l.AutoSize = true;
                l.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                if (dDescs.ContainsKey(id))
                    l.Text = id + ": " + dDescs[id];
                else
                    l.Text = ""+id;
                l.Location = new Point(0, (100 * i));
                panel1.Controls.Add(l);


                Label l2 = new Label();
                l2.AutoSize = true;
                l2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                l2.Text = "URL: ";
                l2.Location = new Point(0, 30 + (100 * i));
                panel1.Controls.Add(l2);


                Label l3 = new Label();
                l3.AutoSize = true;
                l3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                l3.Text = "API: ";
                l3.Location = new Point(0, 60 + (100 * i));
                panel1.Controls.Add(l3);


                Label l4 = new Label();
                l4.AutoSize = true;
                l4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                l4.Text = "";
                l4.Location = new Point(40, 30 + (100 * i));
                panel1.Controls.Add(l4);


                Label l5 = new Label();
                l5.AutoSize = true;
                l5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                l5.Text = "";
                l5.Location = new Point(40, 60 + (100 * i));
                panel1.Controls.Add(l5);

                WaitPictureBox spb = new WaitPictureBox();
                spb.Location = new Point(175, 35 + (100 * i));
                spb.Size = new Size(30, 30);
                spb.Etat = WaitPictureBoxStates.None;
                panel1.Controls.Add(spb);

                Urls.Add(id, dUrls[id]);
                LabelURL.Add(id, l4);
                LabelAPI.Add(id, l5);
                WaitPB.Add(id, spb);
                
                ++i;
            }
            Label lfin = new Label();
            lfin.AutoSize = true;
            lfin.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lfin.Text = "";
            lfin.Location = new Point(0, (100 * i));
            panel1.Controls.Add(lfin);
            ++i;
        }

        private void TestCompareForm_Load(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(DoItAll)).Start();
        }

        private void DoItAll()
        {
            foreach (int id in Urls.Keys)
            {
                WaitPB[id].Etat = WaitPictureBoxStates.Waiting;
                FindItemsAdvancedResponse response = EbayEndPoint.AdvancedSearch(1, 100, new StephaneSearchQuery(Urls[id]));
                
                int numAPI = response.paginationOutput == null ? 0 : response.paginationOutput.totalEntries;
                string src = GatheringUtility.GetPageSource(Urls[id]);
                string nbRes = StringUtility.Extract(src, "<span class='countClass'>", "</span");
                int numURL = nbRes == null ? 0 : int.Parse(nbRes.Replace(",",""));
                WriteNumbers(id, numURL, numAPI);
                WaitPB[id].Etat = numAPI == numURL ? WaitPictureBoxStates.Ok: WaitPictureBoxStates.Bad;
            }
        }
        delegate void I3Handler(int i0, int i1, int i2);
        private void WriteNumbers(int id, int url, int api)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new I3Handler(WriteNumbers), new object[] { id, url, api });
                return;
            }
            LabelURL[id].Text = url.ToString("N0");
            LabelAPI[id].Text = api.ToString("N0");
        }
    }
}
